
import tensorflow as tf
import urllib
from  tensorflow.examples.tutorials.mnist  import  input_data
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model



#验证自己手写图片的识别效果
#导入图片，二值化，并输出模型可识别的格式
def image_to_number(n):
    from PIL import Image
    import numpy as np
    fig, ax = plt.subplots(nrows=int(n/5),ncols=5 )
    ax = ax.flatten()
    image_test = []
    label_test = np.zeros((n,10))#手写图片的lebel
    for i in range(n):
        label_test[i][i] =1#将（0,0）（1,1）等位置赋值为1
        line = []
        img = Image.open("{}.png".format(i))     # 打开一个图片，并返回图片对象
        img = img.convert('L')    # 转换为灰度，img.show()可查看图片
        img = img.resize((28,28))   # 将图片重新以（w,h）尺寸存储
        for y in range(28):
            for x in range(28):
                line.append((255-img.getpixel((x, y)))/255)# getpixel 获取该位置的像素信息
        image_test.append(line)#存储像素点信息


        line = np.array(line)#转化为np.array

        ax[i].imshow(line.reshape(28,28), cmap='Greys', interpolation='nearest')
        # plt.imshow(line.reshape(28,28), cmap='Greys')#显示图片，imshow能够将数字转换为灰度显示出图像
    image_test = np.array(image_test)

    sess = tf.Session()
    saver = tf.train.import_meta_graph('my-test-model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))
    y = tf.placeholder(tf.float32, shape=[None, 10])
    x = tf.placeholder(tf.float32, shape=[None, 784])
    result = sess.run(y, feed_dict={x: image_test})
    print(result)

    plt.show()
    return image_test,label_test



if __name__ == '__main__':
    model=load_model()



